﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CApMRCRecorderUtilities
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.MovieRecorder;

public static class CApMRCRecorderUtilities
{
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern IntPtr FindWindow(string strClassName, string strWindowName);

  [DllImport("user32.dll")]
  public static extern bool GetWindowRect(IntPtr hwnd, ref CApMRCRecorderUtilities.Rect rectangle);

  internal static Rectangle GetApplicationRect()
  {
    IntPtr mainWindowHandle = TxApplicationEx.GetMainWindowHandle(IntPtr.Zero);
    CApMRCRecorderUtilities.Rect rectangle = new CApMRCRecorderUtilities.Rect();
    CApMRCRecorderUtilities.GetWindowRect(mainWindowHandle, ref rectangle);
    return new Rectangle(rectangle.Left, rectangle.Top, rectangle.Right - rectangle.Left, rectangle.Bottom - rectangle.Top);
  }

  internal static Rectangle GetFullScreenRect()
  {
    return new Rectangle(0, 0, SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
  }

  public static ImageSource GetImageSource(Uri path)
  {
    BitmapImage imageSource = (BitmapImage) null;
    try
    {
      imageSource = new BitmapImage(path);
    }
    catch
    {
    }
    return (ImageSource) imageSource;
  }

  public static string NormalizeFilePath(string path)
  {
    return !string.IsNullOrEmpty(path) && !path.EndsWith("\\") ? (path += "\\") : path;
  }

  public static string SerializeSettings(CApMRCRecorderSettings settings)
  {
    using (MemoryStream memoryStream = new MemoryStream())
    {
      using (StreamReader streamReader = new StreamReader((Stream) memoryStream))
      {
        new XmlSerializer(typeof (CApMRCRecorderSettings), new Type[0]).Serialize((Stream) memoryStream, (object) settings);
        memoryStream.Position = 0L;
        return streamReader.ReadToEnd();
      }
    }
  }

  public static CApMRCRecorderSettings DeserializeSettings(string settingsToLoad)
  {
    CApMRCRecorderSettings recorderSettings = (CApMRCRecorderSettings) null;
    if (settingsToLoad != null)
    {
      using (Stream stream = (Stream) new MemoryStream())
      {
        using (StreamWriter streamWriter = new StreamWriter(stream))
        {
          streamWriter.Write(settingsToLoad);
          streamWriter.Flush();
          stream.Position = 0L;
          recorderSettings = new XmlSerializer(typeof (CApMRCRecorderSettings), new Type[0]).Deserialize(stream) as CApMRCRecorderSettings;
        }
      }
    }
    if (recorderSettings == null)
      recorderSettings = new CApMRCRecorderSettings();
    return recorderSettings;
  }

  public static void CheckDefaultSettings(CApMRCRecorderSettings settings)
  {
    List<TxVideoCodec> supportedCodecs = TxMovieRecordingSettings.GetSupportedCodecs();
    if (settings.Codec != 13 && supportedCodecs.Count > 0 && !supportedCodecs.Contains(settings.Codec))
      settings.Codec = supportedCodecs[0];
    if (!(settings.OverlayImagePath != string.Empty))
      return;
    try
    {
      if (new FileInfo(settings.OverlayImagePath).Exists)
        return;
      settings.OverlayImagePath = string.Empty;
    }
    catch
    {
      settings.OverlayImagePath = string.Empty;
    }
  }

  public static Screen GetApplicationFrameScreen()
  {
    return Screen.FromHandle(TxApplicationEx.GetMainWindowHandle(IntPtr.Zero));
  }

  public static uint Percent(this uint value, uint percent)
  {
    return (uint) ((double) value * ((double) percent / 100.0));
  }

  public static int Percent(this int value, uint percent)
  {
    return (int) ((double) value * ((double) percent / 100.0));
  }

  public struct Rect
  {
    public int Left { get; set; }

    public int Top { get; set; }

    public int Right { get; set; }

    public int Bottom { get; set; }
  }
}
